package com.xhtt.modules.app.controller.cfg;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.R;
import com.xhtt.common.validator.ValidatorUtils;
import com.xhtt.common.validator.group.AddGroup;
import com.xhtt.common.validator.group.UpdateGroup;
import com.xhtt.modules.cfg.entity.CfgTaskInfo;
import com.xhtt.modules.cfg.entity.CfgTaskSub;
import com.xhtt.modules.cfg.service.CfgTaskInfoService;
import com.xhtt.modules.cfg.service.CfgTaskSubService;
import com.xhtt.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 任务库 前端控制器
 * </p>
 *
 * @author feipc
 * @since 2019-02-19
 */
@Api(tags = "数据字典-任务库列表", value = "数据字典-任务库列表")
@RestController
@RequestMapping("app/sys/cfg/task")
public class CfgTaskInfoController extends AbstractController {

    @Autowired
    private CfgTaskInfoService cfgTaskInfoService;
    @Autowired
    private CfgTaskSubService cfgTaskSubService;

    /**
     * 获取主任务库列表
     */
    @GetMapping(value = "/list")
    @ApiOperation("获取任务库列表(web/app)")
    public R list(@RequestParam Map<String, Object> params) {
        Object name = params.get("name");
        Object plan = params.get("plan");
        List<CfgTaskInfo> list = cfgTaskInfoService.list(new LambdaQueryWrapper<CfgTaskInfo>()
                .like(!ObjectUtils.isEmpty(name), CfgTaskInfo::getName, name).eq(!ObjectUtils.isEmpty(plan), CfgTaskInfo::getPlanType, plan));
        return R.ok().put(Constant.DATA, list);
    }

    /**
     * 获取子任务库列表
     */
    @GetMapping(value = "/subList")
    @ApiOperation("获取子任务库列表(web/app)")
    public R subList(@RequestParam Map<String, Object> params) {
        Object taskId = params.get("taskId");//父级任务id
        Object plan = params.get("plan");//父级任务模块
        Object planType = params.get("planType");//父级任务类型
        if (!ObjectUtils.isEmpty(plan) && !ObjectUtils.isEmpty(planType)) {
            CfgTaskInfo task = cfgTaskInfoService.getOne(new LambdaQueryWrapper<CfgTaskInfo>().eq(CfgTaskInfo::getPlanType, plan).eq(CfgTaskInfo::getType, planType));
            if (null != task) {
                taskId = task.getId();
            }
        }
        Object name = params.get("name");
        List<CfgTaskSub> list = cfgTaskSubService.list(new LambdaQueryWrapper<CfgTaskSub>().eq(CfgTaskSub::getTaskId, taskId).like(!ObjectUtils.isEmpty(name), CfgTaskSub::getName, name));
//        if (list.size() > 0) {
//            int p = 100 / list.size();
//            list.forEach(cfgTaskSub -> cfgTaskSub.setPercent(p));
//        }
        return R.ok().put(Constant.DATA, list);
    }

    /**
     * 新增分支任务
     */
    @PostMapping(value = "/addSubTask")
    @ApiOperation("新增分支任务(web)")
    public R addSubTask(@RequestBody CfgTaskSub cfgTaskSub) {
        ValidatorUtils.validateEntity(cfgTaskSub, AddGroup.class);
        cfgTaskSub.setType(99);//只能增加其他任务
        cfgTaskSubService.save(cfgTaskSub);
        return R.ok();
    }

    /**
     * 删除分支任务
     */
    @PostMapping(value = "/delete")
    @ApiOperation("删除分支任务(web)")
    public R delete(@RequestBody Integer[] ids) {
        cfgTaskSubService.removeById(ids[0]);
        return R.ok();
    }

    /**
     * 修改子任务
     */
    @PostMapping(value = "/update")
    @ApiOperation("修改子任务(web)")
    public R update(@RequestBody CfgTaskSub cfgTaskSub) {
        ValidatorUtils.validateEntity(cfgTaskSub, UpdateGroup.class);
        cfgTaskSubService.updateById(cfgTaskSub);
        return R.ok();
    }

    /**
     * 获取子任务详情
     */
    @GetMapping(value = "/get/{id}")
    @ApiOperation("获取子任务详情(web/app)")
    public R get(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, cfgTaskSubService.getById(id));
    }
}
